package com.park.web.controller;

import com.park.web.base.baseOperation.BaseController;
import com.park.web.core.wrapper.WrapMapper;
import com.park.web.core.wrapper.Wrapper;
import com.park.web.model.domain.wfFlowchart;
import com.park.web.model.domain.wfProcessnode;
import com.park.web.model.vo.WfProcessNodeVo;
import com.park.web.service.wfFlowchartService;
import com.park.web.service.wfProcessnodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Api(value = "WEB - WfProcessNodeController", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RequestMapping(value = "WfProcessNode", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class WfProcessNodeController extends BaseController {

    @Autowired
    wfProcessnodeService iwfProcessnodeService;
    @Autowired
    wfFlowchartService iwfFlowChartService;

    @PostMapping("/selectAll")
    @ApiOperation(value = "查询所有信息", notes = "查询所有信息")
    public Wrapper<List<WfProcessNodeVo>> selectAll() {
        var charList = iwfFlowChartService.selectAll();
        var nodeList = iwfProcessnodeService.selectAll();
        List<WfProcessNodeVo> wfList = new LinkedList<>();

        for (wfFlowchart chart : charList) {
            List<WfProcessNodeVo.Connection> connectionList = new LinkedList<WfProcessNodeVo.Connection>();
            WfProcessNodeVo wfProcessNodeVo = new WfProcessNodeVo();
            wfProcessNodeVo.setId(chart.getId());
            wfProcessNodeVo.setFlowName(chart.getFlowname());
            wfProcessNodeVo.setFlowType(chart.getFlowtype());
            wfProcessNodeVo.setReMark(chart.getRemark());
            wfProcessNodeVo.setCreateTime(chart.getCreatetime());
            wfProcessNodeVo.setCreateUserID(chart.getCreateuserid());
            var tempNodeList = nodeList.stream().filter(x -> x.getFlowid().equals(chart.getId())).collect(Collectors.toList());
            tempNodeList.forEach(x -> {
                if (null != x.getPreviousnode() && !x.getPreviousnode().isBlank()) {
                    WfProcessNodeVo.Connection connection = new WfProcessNodeVo.Connection();
                    connection.setFromShapeId(x.getPreviousnode());
                    connection.setToShapeId(x.getId());
                    connection.setTitle("下一步");
                    connectionList.add(connection);
                }
            });
            wfProcessNodeVo.setProcessNodeList(tempNodeList);
            wfProcessNodeVo.setConnectionList(connectionList);
            wfList.add(wfProcessNodeVo);
        }
        return WrapMapper.okWithoutMessage(wfList);
    }

    @PostMapping("/saveOrUpdate/{flowId}")
    @ApiOperation(value = "保存更新流程节点", notes = "保存更新流程节点")
    public Wrapper saveOrUpdate(@PathVariable("flowId") String flowId, @RequestBody List<wfProcessnode> list) {
        iwfProcessnodeService.saveOrUpdate(flowId, list);
        return WrapMapper.ok();
    }
}